import type {
  KnowledgePage,
  KnowledgeParams,
  PageParams,
  DoctorPage,
  FollowType,
  TopDep,
  Image,
  ConsultOrderPreData,
  ConsultOrderPreParams,
  PartialConsult,
  ConsultOrderItem,
  ConsultOrderListParams,
  ConsultOrderPage,
} from "@/types/consult";
import type { Patient } from "@/types/user";
import { request } from "@/utils/request";

/**
 * 获取文章列表分页
 * @param params KnowledgeParams
 * @returns Promise
 */
export const getKnowledgePage = (params: KnowledgeParams) => {
  return request<KnowledgePage>("/patient/home/knowledge", "GET", params);
};

/**
 * 获取医生列表分页
 * @param params PageParams
 * @returns Promise
 */
export const getDoctorPage = (params: PageParams) => {
  return request<DoctorPage>("/home/page/doc", "GET", params);
};

/**
 * 关注医生
 * @param id string
 * @param type FollowType
 * @returns Promise
 */
export const followDoctor = (id: string, type: FollowType = "doc") => {
  return request("/like", "POST", { id, type });
};

/**
 * 获取所有科室
 * @returns Promise
 */
export const getAllDep = () => {
  return request<TopDep[]>("/dep/all");
};

/**
 * 上传图片
 * @param file File
 * @returns Promise
 */
export const uploadImage = (file: File) => {
  const fd = new FormData();
  fd.append("file", file);
  return request<Image>("/upload", "POST", fd);
};

/**
 * 拉取预支付订单信息
 * @param params ConsultOrderPreParams
 * @returns Promise
 */
export const getConsultOrderPre = (params: ConsultOrderPreParams) => {
  return request<ConsultOrderPreData>(
    "/patient/consult/order/pre",
    "GET",
    params
  );
};

/**
 * 查询患者详情
 * @param id string
 * @returns Promise
 */
export const getPatientDetail = (id: string) => {
  return request<Patient>(`/patient/info/${id}`);
};

/**
 * 生成订单
 * @param data PartialConsult
 * @returns Promise
 */
export const createConsultOrder = (data: PartialConsult) => {
  return request<{ id: string }>("/patient/consult/order", "POST", data);
};

/**
 * 获取支付地址  0 是微信  1 支付宝
 * @param params Object
 * @returns Promise
 */
export const getConsultOrderPayUrl = (params: {
  paymentMethod: 0 | 1;
  orderId: string;
  payCallback: string;
}) => {
  return request<{ payUrl: string }>("/patient/consult/pay", "POST", params);
};

/**
 * 获取问诊订单单项信息
 * @param orderId string
 * @returns Promise
 */
export const getConsultOrderDetail = (orderId: string) => {
  return request<ConsultOrderItem>("/patient/consult/order/detail", "GET", {
    orderId,
  });
};

/**
 * 查看处方
 * @param id string
 * @returns Promise
 */
export const getPrescriptionPic = (id: string) => {
  return request<{ url: string }>(`/patient/consult/prescription/${id}`);
};

/**
 * 评价问诊
 * @param data Object
 * @returns Promise
 */
export const evaluateConsultOrder = (data: {
  docId: string;
  orderId: string;
  score: number;
  content: string;
  anonymousFlag: 0 | 1;
}) => {
  return request<{ id: string }>("/patient/order/evaluate", "POST", data);
};

/**
 * 获取咨询订单列表
 * @param params ConsultOrderListParams
 * @returns Promise
 */
export const getConsultOrderList = (params: ConsultOrderListParams) => {
  return request<ConsultOrderPage>(
    "/patient/consult/order/list",
    "GET",
    params
  );
};

/**
 * 取消订单
 * @param id string
 * @returns Promise
 */
export const cancelOrder = (id: string) => {
  return request(`/patient/order/cancel/${id}`, "PUT");
};

/**
 * 删除订单
 * @param id string
 * @returns Promise
 */
export const deleteOrder = (id: string) => {
  return request(`/patient/order/${id}`, "DELETE");
};
